package com.team.equ.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.equ.bean.BaseInfo;
import com.team.equ.bean.Msg;
import com.team.equ.bean.WarningType;
import com.team.equ.service.BaseInfoService;
import com.team.equ.service.WarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author KonYan
 * @create 2021-07-16 18:18
 */
@RestController
@RequestMapping("/equi")
@CrossOrigin
public class equiController {

    @Autowired
    private BaseInfoService baseInfoService;

    @Autowired
    private WarTypeService warTypeService;

    @RequestMapping("/getAllBaseInfo/{current}/{limit}")
    public JSONObject getAllBaseInfo(@PathVariable long current,
                                     @PathVariable long limit,
                                     @RequestBody(required = false) BaseInfo baseInfo){

        Page<BaseInfo> page = new Page<>(current,limit);

        QueryWrapper<BaseInfo> wrapper = new QueryWrapper<>();
        if(baseInfo != null){
            String warNo = baseInfo.getWarNo();
            if(!StringUtils.isEmpty(warNo)){
                wrapper.eq("warNo",baseInfo.getWarNo());
            }
        }
        Page<BaseInfo> baseInfoPage = baseInfoService.page(page, wrapper);
        Msg result = Msg.success().add("baseInfo", baseInfoPage);

        return result.getResult();
    }

    @RequestMapping("/getAllWarType")
    public JSONObject getAllWarType(){
        List<WarningType> warTypes = warTypeService.list();
        Msg result = Msg.success().add("warTypes", warTypes);

        return result.getResult();
    }

    @RequestMapping("/getAllWarNum")
    public JSONObject getAllWarNum(){

        QueryWrapper<BaseInfo> wrapper = new QueryWrapper<>();

        int warNum = baseInfoService.count();

        wrapper.groupBy("war_area");

        List<Map<String, String>> warArea = baseInfoService.getWarArea();

        Msg result = Msg.success().add("warNum", warNum).add("warArea",warArea);

        return result.getResult();
    }
}
